package com.example.demo.controller;

import ch.qos.logback.core.util.StringUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-05-06
 * Time:21:44
 */
@RequestMapping("/user")
@RestController
public class UserControler {
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session){
        /*参数校验*/
//        if(username==null||username.length()==0||
//        password==null||password.length()==0){
//            return false;
//        }
        if (!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){
            return false;
        }
        //判断密码是否正确，上面已经做了判空的处理
        if("admin".equals(userName)&&"admin".equals(password)){//admin写在前面是为了防null
            //设置session
            session.setAttribute("userName",userName);
            return true;
        }
        return false;
    }
    @RequestMapping("/index")
    public String getUsername(@SessionAttribute("userName") String userName){
        return userName;
    }
}
